#include <slothttpd/config/create_matcher.hpp>

#include <stdexcept>

#include <boost/exception/all.hpp>
#include <boost/make_shared.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/regex.hpp>

namespace slothttpd {
namespace config {

bool match_extension(char_range_t seq, const std::string &ext)
{
    return boost::algorithm::ends_with(seq, ext);
}

bool match_regex(char_range_t seq, boost::regex &regex)
{
    return boost::regex_match(seq.begin(), seq.end(), regex);
}

matcher_type create_matcher(const std::string &pattern)
{
    static const std::string prefix("ext:");
    
    if (boost::algorithm::starts_with(pattern, prefix)) {
        const std::string ext = boost::algorithm::erase_head_copy(pattern, prefix.size());
        
        return boost::bind(&match_extension, _1, ext);
    } else {
        return boost::bind(&match_regex, _1, boost::regex(pattern));
    }
}

}
}
